<?php

namespace common\helpers;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * 自定义实用工具类
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class SqlDataTransfer extends \yii\base\Component {

    const CRLF_CHAR = "\r\n";

    public $version = '';
    public $dsn = '';
    public $port = '3306';
    public $serverType = 'MYSQL';
    //
    private $content = '';
    private $host = 'localhost';
    private $databaseName = '';
    private $filename = 'remote';

    public function __construct($config = array()) {
        parent::__construct($config);

        $this->dsn = str_replace(['mysql:host=', 'dbname='], '', $this->dsn);
        if ($this->dsn) {
            list($this->host, $this->databaseName) = explode(';', $this->dsn);
        }
    }

    public function getCreateTableSql() {
        $sql = "show create table " . $tablename . ';';
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    public function sendAsFile($filename = false) {
        if (!$filename) {
            $filename = $this->filename;
        }
        return Yii::$app->response->sendContentAsFile($this->content, $filename);
    }

    public function note($data = []) {
        $this->line("/*");
        $this->line("MySQL Data Transfer from " . Yii::$app->name);
        $this->line();
        $data = ArrayHelper::merge($this->baseInfo, $data);
        foreach ($data as $key => $vls) {
            $line = str_pad($key, 22, " ", STR_PAD_RIGHT) . ": " . $vls;
            $this->content .= $this->line($line);
        }
        $this->line();
        $this->line("Date: " . date('Y-m-d H:i:s'));
        $this->line("*/");
        $this->line();
    }

    public function table($data) {
        $tablename = array_shift($data);
        $sql = array_shift($data);
        $this->line();
        $this->beginTable($tablename);
        $this->line(str_replace("\n", self::CRLF_CHAR, $sql) . ";");
        $this->line();
    }

    public function beginTable($name) {
        $this->hyphen("----------------------------");
        $this->hyphen("Table structure for `{$name}`");
        $this->hyphen("----------------------------");
        $this->line("DROP TABLE IF EXISTS `{$name}`;");
        $this->filename = $name . '.sql';
    }

    public function line($string = '') {
        $this->content .= $string . self::CRLF_CHAR;
    }

    public function hyphen($string = '') {
        $this->content .= "-- " . $string . self::CRLF_CHAR;
    }

    public function getContent() {
        return $this->content;
    }

    public function getServerVersion() {
        $versionNumber = '';
        $version = explode('.', $this->version);
        array_walk($version, function($v, $k) use (&$versionNumber) {
            if ($k) {
                $v = sprintf('%02d', $v);
            }
            $versionNumber .= $v;
        });
        return $versionNumber;
    }

    public function getBaseInfo() {
        return [
            "Source Server" => $this->host,
            "Source Server Version" => $this->serverVersion,
            "Source Host" => $this->host . ':' . $this->port,
            "Source Database" => $this->databaseName . SqlDataTransfer::CRLF_CHAR,
            "Target Server Type" => "MYSQL",
            "Target Server Version" => $this->serverVersion,
            "File Encoding" => "65001",
        ];
    }

    public function getHost() {
        return $this->host;
    }

    public function getDatabaseName() {
        return $this->databaseName;
    }

}
